<?php
namespace App\Http\Controllers;

use App\Models\ActivityPhoto;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ActivityPhotoController extends CommonController
{

    /**
     * 获取活动流程图片管理界面
     *
     * @return Response
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();
        $lists = ActivityPhoto::where('activity_id', $data['id'])->paginate(10);

        return view('activityphoto.index')->with("lists", $lists)->with("actions", $this->actions)->with("activity_id", $data['id']);
    }

    /**
     * 获取增加图片的界面
     *
     */
    public function getAddPhoto(Request $request)
    {
        $data = $request->input();
        $activity_id = $data['activity_id'];

        return view('activityphoto.create')->with('activity_id', $activity_id);
    }

    /**
     * 执行图片地址添加
     */
    public function postInsert(Request $request)
    {
        $data = $request->input();

        ActivityPhoto::create(['activity_id' => $data['activity_id'], 'type' => 'photo', 'image' => $data['image'], 'description' => $data['description']]);

        return AjaxCallbackMessage('流程图新增成功', true, url('activityphoto/index?id=' . $data['activity_id']));
    }

    /**
     * 获取图片编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();

        $list = ActivityPhoto::where('id', $data['id'])->first();

        return view('activityphoto.edit')->with('list', $list);
    }

    /**
     * 编辑一个流程图
     */
    public function postEdit(Request $request)
    {
        $data = $request->input();

        ActivityPhoto::where('id', $data['id'])->update(['image'=>$data['image'], 'description'=>$data['description'], 'status'=>$data['status']]);

        return AjaxCallbackMessage('流程图编辑成功', true, url('activityphoto/index?id=' . $data['activity_id']));
    }

    /**
     * 删除一张图片
     */
    public function postDelPhoto(Request $request)
    {
        $data = $request->input();
        $photo = ActivityPhoto::where('id', $data['pid'])->first();
        ActivityPhoto::where('id', $data['pid'])->delete();

        return AjaxCallbackMessage('图片删除成功', true, url('activityphoto/index?id=' . $photo->activity_id));
    }
}